<?php

require_once("database.php");
require_once("exception.php");

class Category
{
	private $id;
	private $name;
	private $parentId;
	
	function __construct($id = 0,
						 $name = "",
						 $parentId = 0)
	{
		$this->id = $id;
		$this->name = $name;
		$this->parentId = $parentId;
	}
	
	public function setId($id) {$this->id = $id;}
	public function setName($name) {$this->name = $name;}
	public function setParentId($parentId) {$this->parentId = $parentId;}
	
	public function getId() {return $this->id;}
	public function getName() {return $this->name;}
	public function getParentId() {return $this->parentId;}
}

class CategoryDAO
{
	private $db;
	function __construct()
	{
		$this->db = new Database();
	}
	
	public function addTopCategory($category)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("insert into ESCommodityType(name) values(?)", array($category->getName()));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function getAllCategoryList()
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESCommodityType", array());
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$id = $rs->value("id");
				$name = $rs->value("name");
				$category = new Category($id, $name);
				$list[$id] = $category;
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
	
	public function getTopCategoryList()
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESCommodityType where parentId is null", array());
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$id = $rs->value("id");
				$name = $rs->value("name");
				$category = new Category($id, $name);
				$list[$id] = $category;
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
	
	public function addSubCategory($category)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("insert into ESCommodityType(name, parentId) values(?, ?)", array($category->getName(), $category->getParentId()));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function getSubCategoryList($parentId)
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESCommodityType where parentId = ?", array($parentId));
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$id = $rs->value("id");
				$name = $rs->value("name");
				$parentId = $rs->value("parentId");
				$category = new Category($id, $name, $parentId);
				$list[$id] = $category;
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
	
	public function getCategoryChain($categoryId)
	{
		$rslt = "";
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$currentId = $categoryId;
			while(true)
			{
				$prepStmt = $conn->prepareStatement("select * from ESCommodityType where id = ?", array($currentId));
				$rs = $prepStmt->executeQuery();
				if($rs->next())
				{
					$id = $rs->value("id");
					$name = $rs->value("name");
					$parentId = $rs->value("parentId");
					if($currentId != $categoryId)
						$rslt = "<a href = \"list.php?categoryid=$id\">$name</a>&nbsp;&gt;&nbsp;" . $rslt;
					else
						$rslt = "$name";
				}
				else
					break;
					
				$currentId = $parentId;
			}
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	public function getCategoryById($id)
	{
		$category = new Category();
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESCommodityType where id = ?", array($id));
			$rs = $prepStmt->executeQuery();
			if($rs->next())
			{
				$category->setId($rs->value("id"));
				$category->setName($rs->value("name"));
				$category->setParentId($rs->value("parentId"));
			}
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $category;
	}
}

?>